package com.jdd.dao.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.common.api.vo.Result;
import com.jdd.common.util.ResultGenerator;
import com.jdd.dao.entity.*;
import com.jdd.dao.service.ITFloorService;
import com.jdd.dao.service.ITUnitService;
import com.jdd.dao.vo.SpaceVo;
import com.jdd.modules.device.entity.TDevice;
import com.jdd.modules.device.service.TDeviceService;
import com.jdd.modules.people.model.TProjectTreeModel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2022-09-19
 */
@RestController
@RequestMapping("/unit")
public class TUnitController {
    @Resource
    private ITUnitService unitService;

    @Resource
    private ITFloorService floorService;
    @Resource
    private TDeviceService deviceService;



    @GetMapping("/getUnitList")
    public Result<List<TUnit>> geTHouseList(@RequestParam("buildingId")String buildingId){

        LambdaQueryWrapper<TUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TUnit::getBuildingId,buildingId);
        List<TUnit> list = unitService.list(wrapper);
        Result<List<TUnit>> result = new Result<>();
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }

    @PostMapping("/changeName")
    public Result changeName(@RequestBody TUnit tUnit) {
        TUnit unit = unitService.getById(tUnit.getId());
        LambdaQueryWrapper<TUnit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TUnit::getBuildingId,unit.getBuildingId());
        queryWrapper.eq(TUnit::getUnitName,tUnit.getName());
        int count = unitService.count(queryWrapper);
        if (count>0){
            return ResultGenerator.genFailResult("修改名称已经存在");
        }
        String deviceParentCode = unit.getParentCode()+ "U" + unit.getCode();
        deviceService.updateDeviceAlias(deviceParentCode,2,unit.getParentCode(),tUnit.getName());
        tUnit.setUnitName(tUnit.getName());
        unitService.saveOrUpdate(tUnit);
        return ResultGenerator.genSuccessResult(tUnit);
    }

    @DeleteMapping("/delete")
    public Result deleteUnit(@RequestParam(name = "id",required = true)String id){
        int count = floorService.count(new QueryWrapper<TFloor>().eq("unit_id", id));
        if (count > 0) {
            return ResultGenerator.genFailResult("该区域已绑定楼层");
        }
        int count1 = deviceService.count(new QueryWrapper<TDevice>().eq("floor_id", id));
        if (count1 > 0) {
            return ResultGenerator.genFailResult("该区域已绑定设备");
        }
        unitService.removeById(id);
        return ResultGenerator.genSuccessResult("删除成功");
    }
    @PostMapping(value = "/add")
    public Result<?> addNew(@RequestBody SpaceVo space) {
        return unitService.addNew(space);
    }

    /*传入项目id，返回该项目id下的子节点*/
    @GetMapping("/getChildren")
    public Result<List<TProjectTreeModel>> getChildren(@RequestParam(name = "id")String id){
        List<TProjectTreeModel> tProjectTreeModels = new ArrayList<>();
        LambdaQueryWrapper<TFloor> tAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tAreaLambdaQueryWrapper.eq(TFloor::getUnitId,id);
        List<TFloor> floors = floorService.list(tAreaLambdaQueryWrapper);
        for (TFloor tFloor : floors) {
            tProjectTreeModels.add(new TProjectTreeModel(tFloor));
        }
        return ResultGenerator.genSuccessResult(tProjectTreeModels);
    }
    @GetMapping("/getChildrenWithDevice")
    public Result<List<TProjectTreeModel>> getChildrenWithTDevice(@RequestParam(name = "id")String id){
        List<TProjectTreeModel> tProjectTreeModels = new ArrayList<>();
        LambdaQueryWrapper<TFloor> tAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tAreaLambdaQueryWrapper.eq(TFloor::getUnitId,id);
        TUnit tUnit = unitService.getById(id);
        List<TFloor> floors = floorService.list(tAreaLambdaQueryWrapper);
        LambdaQueryWrapper<TDevice> deviceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceLambdaQueryWrapper.eq(TDevice::getUnitId, id);
        deviceLambdaQueryWrapper.eq(TDevice::getFloorId,"").or().isNull(TDevice::getFloorId);
        List<TDevice> deviceList = deviceService.list(deviceLambdaQueryWrapper);
        for (TDevice device : deviceList) {
            tProjectTreeModels.add(new TProjectTreeModel(device,tUnit));
        }
        for (TFloor tFloor : floors) {
            tProjectTreeModels.add(new TProjectTreeModel(tFloor));
        }
        return ResultGenerator.genSuccessResult(tProjectTreeModels);
    }
}
